﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Partner
{
    public partial class frmCustomer : frmBase
    {
        #region Contructor
        public frmCustomer()
        {
            InitializeComponent();
        }
        #endregion

        #region Variable
        ManageTicket.Library.Control.ExecuteControl exeCtrl;

        //permission
        bool bAdd = false;
        bool bEdit = false;
        bool bDelete = false;
        bool bCancel = false;
        bool bSave = false;
        bool bFunction = false;
        bool bView = false;
        bool bFind = false;
        bool bExport = false;
        bool bSendmail = false;
        bool bPrint = false;

        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        private Business.Customer boCustomer = new Business.Customer();
        #endregion

        #region Event
        private void frmCustomer_Load(object sender, EventArgs e)
        {
            //execute control
            exeCtrl = new ManageTicket.Library.Control.ExecuteControl();
            exeCtrl.Dock = DockStyle.Fill;
            exeCtrl.lblDescription.Text = "Cập nhật danh sách khách hàng";
            pnlExe.Controls.Add(exeCtrl);
            
            //permission
            clsCommonFunc.GetPermission(ref bAdd, ref bEdit, ref bDelete, ref bCancel, ref bSave, ref bFunction, ref bView, ref bFind, ref bExport, ref bSendmail, ref bPrint, frmMain.CurrentUser.UserId, "FORM_05");

            //get code type
            clsCommonFunc.GetListCodeType(cboBank, "BANK");
            clsCommonFunc.GetListCodeType(cboKindOfCustomer, "KOC");
            clsCommonFunc.GetListCodeType(cboPersonRepresentNationality, "NATION");
            clsCommonFunc.GetListCodeType(cboPersonContactNationality, "NATION");

            //set status
            LoadStatusOnce();
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                ArrayList arrResult = new ArrayList();
                if (objMethod.IsInteger(txtSearch.Text))
                {
                    DataObjects.Customer oCus = boCustomer.GetObjectByPK(txtSearch.Text);
                    if (oCus != null)
                    {
                        arrResult.Add(oCus);
                    }
                }
                else
                {
                    ArrayList arr1 = boCustomer.GetListQuickSearch(txtSearch.Text);
                    if (arr1 != null)
                    {
                        if (arr1.Count == 1)
                        {
                            arrResult.Add(arr1[0]);
                        }
                        else if (arr1.Count > 1)
                        {
                            frmQuickSearchCustomer frmQS = new frmQuickSearchCustomer(arr1);
                            frmQS.ShowDialog();
                            foreach (DataObjects.Customer oCus in frmQS.arrResult)
                            {
                                arrResult.Add(oCus);
                            }
                        }
                    }
                }

                ListViewItem item;
                int firstItem = 0;
                foreach (DataObjects.Customer oCus in arrResult)
                {
                    if (!IsInListViewCustomer(oCus))
                    {
                        item = new ListViewItem();
                        item.Text = oCus.CustomerId;
                        item.SubItems.Add(oCus.CustomerName);
                        lvwCustomer.Items.Add(item);
                        if (firstItem == 0)
                        {
                            item.Selected = true;
                        }
                        firstItem++;
                    }
                }
            }
        }

        private void lvwCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwCustomer.SelectedItems.Count > 0)
            {
                DataObjects.Customer oCus = boCustomer.GetObjectByPK(lvwCustomer.SelectedItems[0].Text);
                if (oCus != null)
                {
                    FillData(oCus);
                    LoadStatus(true, true, true, true);
                }
            }
            else
            {
                LoadStatus(true, false, true, true);
            }
        }
        #endregion

        #region Execute
        public override void Add()
        {
            txtCustomerId.Text = clsCommonFunc.RuleNumber5(boCustomer.GetMaxID() + 1);
            objMethod.ClearText(new Control[] { txtCustomerName, txtAddress, txtPhoneWork, txtPhoneExtension, txtFax, txtTaxCode, txtBankAccount, txtSpecialRequire, txtRemark, txtPersonRepresent, txtPersonRepresentMobile, txtPersonRepresentEmail, txtPersonContact, txtPersonContactMobile, txtPersonContactEmail });
            chkIsProvider.Checked = false;
            txtCustomerName.Focus();
        }

        public override void Delete()
        {
            try
            {
                if (txtCustomerId.Text == "")
                    return;

                if (MessageBox.Show("Bạn có thực sự muốn xóa?", "Aquarius", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //Check Existing in table
                    //if (this.CheckExitToDelete(oDept.DEPT_CODE)) {
                    //    MessageBox.Show(".", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    return;
                    //}

                    //delete
                    boCustomer.DeleteByPK(txtCustomerId.Text);
                    RemoveItem(txtCustomerId.Text);
                    objMethod.ClearText(new Control[] { txtCustomerName, txtAddress, txtPhoneWork, txtPhoneExtension, txtFax, txtTaxCode, txtBankAccount, txtSpecialRequire, txtRemark, txtPersonRepresent, txtPersonRepresentMobile, txtPersonRepresentEmail, txtPersonContact, txtPersonContactMobile, txtPersonContactEmail });
                    MessageBox.Show("Xóa khách hàng thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public override void Save()
        {
            if (txtCustomerId.Text == "")
            {
                return;
            }
            if (CheckValid())
            {
                try
                {
                    DataObjects.Customer oCus = new DataObjects.Customer();
                    oCus.CustomerId = txtCustomerId.Text;
                    oCus.CustomerName = txtCustomerName.Text;
                    oCus.Address = txtAddress.Text;
                    oCus.PhoneWork = txtPhoneWork.Text;
                    oCus.PhoneExtension = txtPhoneExtension.Text;
                    oCus.Fax = txtFax.Text;
                    oCus.TaxCode = txtTaxCode.Text;
                    oCus.BankAccount = txtBankAccount.Text;
                    oCus.BankCode = cboBank.SelectedValue != null ? cboBank.SelectedValue.ToString() : "";
                    oCus.KindOfCustomer = cboKindOfCustomer.SelectedValue != null ? cboKindOfCustomer.SelectedValue.ToString() : "";
                    oCus.IsProvider = chkIsProvider.Checked;
                    oCus.SpecialRequire = txtSpecialRequire.Text;
                    oCus.Remark = txtRemark.Text;
                    oCus.PersonRepresent = txtPersonRepresent.Text;
                    oCus.PersonRepresentNational = cboPersonRepresentNationality.SelectedValue != null ? cboPersonRepresentNationality.SelectedValue.ToString() : "";
                    oCus.PersonRepresentMobile = txtPersonRepresentMobile.Text;
                    oCus.PersonRepresentEmail = txtPersonRepresentEmail.Text;
                    oCus.PersonContact = txtPersonContact.Text;
                    oCus.PersonContactNational = cboPersonContactNationality.SelectedValue != null ? cboPersonContactNationality.SelectedValue.ToString() : "";
                    oCus.PersonContactMobile = txtPersonContactMobile.Text;
                    oCus.PersonContactEmail = txtPersonContactEmail.Text;
                    oCus.Status = "CUR";

                    DataObjects.Customer oExistEmp = boCustomer.GetObjectByPK(oCus.CustomerId);
                    if (oExistEmp == null)
                    {
                        boCustomer.Insert(oCus);
                        AddItem(oCus);
                    }
                    else
                    {
                        boCustomer.Update(oCus);
                        RemoveItem(oCus.CustomerId);
                        AddItem(oCus);
                    }

                    MessageBox.Show("Cập nhật khách hàng thành công.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + "Không thể cập nhật khách hàng.", "Aquarius Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public override void Find()
        {
            frmSearchCustomer frmSearch = new frmSearchCustomer();
            frmSearch.ShowDialog();

            ArrayList arrResult = frmSearch.arrResult;
            ListViewItem item;
            int firstItem = 0;

            foreach (DataObjects.Customer oCus in arrResult)
            {
                if (!IsInListViewCustomer(oCus))
                {
                    item = new ListViewItem();
                    item.Text = oCus.CustomerId;
                    item.SubItems.Add(oCus.CustomerName);
                    lvwCustomer.Items.Add(item);

                    if (firstItem == 0)
                    {
                        item.Selected = true;
                    }
                    firstItem++;
                }
            }
        }
        #endregion

        #region Method
        private bool IsInListViewCustomer(DataObjects.Customer oCus)
        {
            foreach (ListViewItem item in lvwCustomer.Items)
            {
                if (item.Text == oCus.CustomerId)
                {
                    return true;
                }
            }
            return false;
        }

        private void LoadStatusOnce()
        {
            exeCtrl.btnEdit.Enabled = false;
            exeCtrl.btnCancel.Enabled = false;
            exeCtrl.btnView.Enabled = false;
            exeCtrl.btnFunction.Enabled = false;
            exeCtrl.btnExport.Enabled = false;
            exeCtrl.btnSendmail.Enabled = false;
            exeCtrl.btnPrint.Enabled = false;

            exeCtrl.btnEdit.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnCancel.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnView.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnFunction.BackColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnExport.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnSendmail.ButtonColor = Color.FromArgb(171, 171, 171);
            exeCtrl.btnPrint.ButtonColor = Color.FromArgb(171, 171, 171);

            exeCtrl.btnEdit.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnCancel.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnView.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnFunction.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnExport.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnSendmail.ForeColor = Color.FromArgb(241, 244, 251);
            exeCtrl.btnPrint.ForeColor = Color.FromArgb(241, 244, 251);
        }

        private void LoadStatus(bool blAdd, bool blDelete, bool blSave, bool blFind)
        {
            exeCtrl.btnAdd.Enabled = blAdd & bAdd;
            exeCtrl.btnDelete.Enabled = blDelete & bDelete;
            exeCtrl.btnSave.Enabled = blSave & bSave;
            exeCtrl.btnFind.Enabled = blFind & bFind;

            exeCtrl.btnAdd.ButtonColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnDelete.ButtonColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnSave.ButtonColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);
            exeCtrl.btnFind.ButtonColor = exeCtrl.btnFind.Enabled ? Color.FromArgb(0, 163, 211) : Color.FromArgb(171, 171, 171);

            exeCtrl.btnAdd.ForeColor = exeCtrl.btnAdd.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnDelete.ForeColor = exeCtrl.btnDelete.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnSave.ForeColor = exeCtrl.btnSave.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
            exeCtrl.btnFind.ForeColor = exeCtrl.btnFind.Enabled ? Color.FromArgb(0, 0, 0) : Color.FromArgb(241, 244, 251);
        }

        private void FillData(DataObjects.Customer oCus)
        {
            try
            {
                txtCustomerId.Text = oCus.CustomerId;
                txtCustomerName.Text = oCus.CustomerName;
                //person represent
                txtPersonRepresent.Text = oCus.PersonRepresent;
                if (oCus.PersonRepresentNational != null && oCus.PersonRepresentNational != "")
                    cboPersonRepresentNationality.SelectedValue = oCus.PersonRepresentNational;
                txtPersonRepresentMobile.Text = oCus.PersonRepresentMobile;
                txtPersonRepresentEmail.Text = oCus.PersonRepresentEmail;
                //person contact
                txtPersonContact.Text = oCus.PersonContact;
                if (oCus.PersonContactNational != null && oCus.PersonContactNational != "")
                    cboPersonContactNationality.SelectedValue = oCus.PersonContactNational;
                txtPersonContactMobile.Text = oCus.PersonContactMobile;
                txtPersonContactEmail.Text = oCus.PersonContactEmail;
                txtAddress.Text = oCus.Address;
                txtPhoneWork.Text = oCus.PhoneWork;
                txtPhoneExtension.Text = oCus.PhoneExtension;
                txtFax.Text = oCus.Fax;
                txtTaxCode.Text = oCus.TaxCode;
                txtBankAccount.Text = oCus.BankAccount;
                if (oCus.BankCode != null && oCus.BankCode != "")
                    cboBank.SelectedValue = oCus.BankCode;
                if (oCus.KindOfCustomer != null && oCus.KindOfCustomer != "")
                    cboKindOfCustomer.SelectedValue = oCus.KindOfCustomer;
                chkIsProvider.Checked = oCus.IsProvider != null ? oCus.IsProvider.Value : false;
                txtSpecialRequire.Text = oCus.SpecialRequire;
                txtRemark.Text = oCus.Remark;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool CheckValid()
        {
            if (txtCustomerName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Vui lòng nhập tên công ty.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtCustomerName.Focus();
                return false;
            }
            if (txtPersonRepresentEmail.Text.Trim().Length > 0)
            {
                if (!objMethod.CheckEmail(txtPersonRepresentEmail.Text))
                {
                    MessageBox.Show("Email người đại diện không hợp lệ.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPersonRepresentEmail.Focus();
                    return false;
                }
            }
            if (txtPersonContactEmail.Text.Trim().Length > 0)
            {
                if (!objMethod.CheckEmail(txtPersonContactEmail.Text))
                {
                    MessageBox.Show("Email người liên lạc không hợp lệ.", "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPersonContactEmail.Focus();
                    return false;
                }
            }

            return true;
        }

        private void AddItem(DataObjects.Customer oCus)
        {
            ListViewItem item = new ListViewItem();
            item.Text = oCus.CustomerId;
            item.SubItems.Add(oCus.CustomerName);
            lvwCustomer.Items.Add(item);
        }

        private void RemoveItem(string empId)
        {
            foreach (ListViewItem item in lvwCustomer.Items)
            {
                if (item.Text == empId)
                {
                    lvwCustomer.Items.Remove(item);
                    return;
                }
            }
        }
        #endregion

        
    }
}
